package com.cndw.kungfu.model.player.domain;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.cndw.kungfu.config.ConstantsConfig;
import com.cndw.kungfu.config.ConstantsIntKey;
import com.cndw.kungfu.config.ConstantsLog;
import com.cndw.kungfu.config.ConstantsModel;
import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.ext.TimeProcesser;
import com.cndw.kungfu.ext.XmlProcesser;
import com.cndw.kungfu.model.sys.LogServer;

public class Office {
	
	/**
	 * 官职等级
	 */
	private int level;
	/**
	 * 官职名称
	 */
	private String title;
	/**
	 * 升级所需战功
	 */
	private int need;
	/**
	 * 伤害加成
	 */
	private int hurtCoff;
	/**
	 * 星级
	 */
	private int star;
	/**
	 * 每日消耗战功
	 */
	private int costEveryDay;
	/**
	 * 俸禄
	 */
	private int salary;
	/**
	 * 官职名称颜色
	 */
	private int color;
	
	public int getLevel() {
		return level;
	}
	public void setLevel(int level) {
		this.level = level;
	}
	public String getTitle() {
		return title;
	}
	public int getStar() {
		return star;
	}
	public void setStar(int star) {
		this.star = star;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public int getNeed() {
		return need;
	}
	public void setNeed(int need) {
		this.need = need;
	}
	public int getHurtCoff() {
		return hurtCoff;
	}
	public void setHurtCoff(int hurtCoff) {
		this.hurtCoff = hurtCoff;
	}
	public int getCostEveryDay() {
		return costEveryDay;
	}
	public void setCostEveryDay(int costEveryDay) {
		this.costEveryDay = costEveryDay;
	}
	public int getSalary() {
		return salary;
	}
	public void setSalary(int salary) {
		this.salary = salary;
	}
	public int getColor() {
		return color;
	}
	public void setColor(int color) {
		this.color = color;
	}
	

	private static final String configFile = "office.xml";

	private static final Map<Integer, Office> officeMap = new ConcurrentHashMap<Integer, Office>();

	static {
		load();
	}

	public static synchronized void load() {
		String fileName = ConstantsConfig.getLocaleResource() + configFile;
		List<Office> officeList = XmlProcesser.processXml(Office.class, fileName);
		for (Office e : officeList) {
			officeMap.put(e.getLevel(), e);
		}
	}

	public static synchronized void clear() {
		officeMap.clear();
		load();
	}

	public static Collection<Office> getAll() {
		return officeMap.values();
	}
	
	public static Office getOne(int level) {
		return officeMap.get(level);
	}
	
	public static int getHurtCoffByLv(int level) {
		return officeMap.get(level).getHurtCoff();
	}
	public static int getSalaryByLv(int level) {
		return officeMap.get(level).getSalary();
	}
	
	public static int getLvByAchive(int achive) {
		for (int lv = 1; lv < officeMap.size(); lv++) {
			if (achive < officeMap.get(lv).getNeed()) {
				return lv - 1;
			}
		}
		return ConstantsModel.officeLvMax;
	}
	
	/**
	 * 扣除玩家战功
	 * @param player
	 */
	public static void deAchive(Player player) {
		int officeLv = player.getDataInfo().intDataGet(ConstantsIntKey.office);
		if (officeLv < ConstantsModel.officeLv24) {
			return;
		}
		
		int curTime = TimeProcesser.getMorningTime();
		int lastDeTime = player.getDataInfo().intDataGet(ConstantsIntKey.deAc);
		int days = (curTime - lastDeTime) / 86400;
		if (days <= 0) {
			return;
		}
		Office curOffice = null;
		
		int archiveDe = 0;
		while (days > 0) {
			curOffice = Office.getOne(officeLv);
			archiveDe = curOffice.getCostEveryDay();
			int curAchv = player.getDataInfo().intDataGet(ConstantsIntKey.achv);
			if (officeLv >= ConstantsModel.officeLv24 && (curAchv - archiveDe) < Office.getOne(ConstantsModel.officeLv24).getNeed()) {
				curAchv = Office.getOne(ConstantsModel.officeLv24).getNeed();
				days = 0;
			}
			player.getDataInfo().alterAchive(-archiveDe);
			officeLv = player.getDataInfo().intDataGet(ConstantsIntKey.office);
			days--;
		}
		
		player.getDataInfo().intDataSet(ConstantsIntKey.deAc, TimeProcesser.getMorningTime());
		player.getDataInfo().commitSync();
		int curAchive = player.getDataInfo().intDataGet(ConstantsIntKey.achv);
		LogServer.recordLog(player, ConstantsLog.player, 30402, curOffice.getTitle(), curOffice.getStar(), days, archiveDe, curAchive);
		
	}
	
}
